package org.cloudie.docs.editor.client.content.dialogs;

import org.cloudie.docs.editor.client.commands.generic.DesktopConfigureCommand;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.widgets.ColorBox;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

/**
 * A dialog window containing a new Icon form.
 */
public class PreferencesDialog extends FormDialog {

  protected static PreferencesDialog instance;
  
  public static PreferencesDialog get(
	    String bgColor, String bgImage, String iconTextColor,
	    String iconBgColor) {
    if (instance == null) {
      instance = new PreferencesDialog();
    }
    instance.bgColor.setValue(bgColor);
    instance.bgImage.setValue(bgImage);
    instance.iconTextColor.setValue(iconTextColor);
    instance.iconBgColor.setValue(iconBgColor);
    return instance;
  }

  private Button apply;
  private TextBox bgImage;
  private ColorBox bgColor, iconTextColor, iconBgColor;
  private CheckBox isTiled;
  private ListBox bgPosition;
  
  /**
   * Constructs a New Icon dialog.
   */
  protected PreferencesDialog() {
    super("Desktop Preferences", true);
    addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
      }
    });
    buildForm();
  }
  
  /**
   * Builds the New Icon form.
   */
  @Override
  protected void buildForm() {
    bgColor = new ColorBox();
    bgColor.setPixelSize(100, 15);
    iconTextColor = new ColorBox();
    iconTextColor.setPixelSize(100, 15);
    iconBgColor = new ColorBox();
    iconBgColor.setPixelSize(100, 15);
    bgImage = new TextBox();
    bgImage.setWidth("300px");
    isTiled = new CheckBox("Tile the background image");
    isTiled.setValue(true);
    bgPosition = new ListBox();
    bgPosition.addItem("Top Left", "left top");
    bgPosition.addItem("Mid Left", "left center");
    bgPosition.addItem("Bottom Left", "left bottom");
    bgPosition.addItem("Top Center", "center top");
    bgPosition.addItem("Mid Center", "center center");
    bgPosition.addItem("Bottom Center", "center bottom");
    bgPosition.addItem("Top Right", "right top");
    bgPosition.addItem("Mid Right", "right center");
    bgPosition.addItem("Bottom Right", "right bottom");
    bgPosition.setItemSelected(0, true);
    submit.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          CommandEvent.fire(new DesktopConfigureCommand(
            	bgColor.getValue(), bgImage.getValue(),
            	bgPosition.getValue(bgPosition.getSelectedIndex()),
            	isTiled.getValue() ? "repeat" : "no-repeat",
            	iconTextColor.getValue(), iconBgColor.getValue()));
          hide();
      }
    });
    apply = new Button("Apply");
    apply.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          CommandEvent.fire(new DesktopConfigureCommand(
                bgColor.getValue(), bgImage.getValue(),
                bgPosition.getValue(bgPosition.getSelectedIndex()),
                isTiled.getValue() ? "repeat" : "no-repeat",
                iconTextColor.getValue(), iconBgColor.getValue()));
      }
    });
    buttonPanel.insert(apply, 1);
    cancel.addClickHandler(new ClickHandler(){
        public void onClick(ClickEvent event) {
          hide();
        }
    });
    HorizontalPanel bp = new HorizontalPanel();
    bp.setSpacing(10);
    bp.add(submit);
    bp.add(apply);
    bp.add(cancel);
    addField(bgColor, "Background Color:");
    addField(iconTextColor, "Icon Text Color:");
    addField(iconBgColor, "Icon Background Color:");
    addField(bgImage, "Background Image URL:");
    addField(bgPosition, "Background Position:");
    addField(isTiled, "");
    addField(bp);
    super.buildForm();
  }
  
  @Override
  public void resetForm() {
    bgImage.setValue("");
	bgColor.setValue(null);
	iconTextColor.setValue(null);
    iconBgColor.setValue(null);
	isTiled.setValue(true);
	bgPosition.setSelectedIndex(0);
  }
}
